package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.ExpressInfoMapper;
import com.example.demo.base.ArrayCountUtils;
import com.example.demo.base.Result;
import com.example.demo.entity.AgriProduct;
import com.example.demo.entity.ExpressInfo;
import com.example.demo.entity.ProductInfo;
import com.example.demo.entity.ProductLog;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.example.demo.base.ArrayCountUtils.merge;

@RestController
@RequestMapping("/expressInfo")
public class ExpressInfoController {
    @Resource
    ExpressInfoMapper expressInfoMapper;


    /**
     * 查询全部信息
     *
     * @return
     */
    @GetMapping("/all")
    public Result<?> findAll() {
        return Result.success(expressInfoMapper.selectList(null));
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/pageAll")
    public Result<?> findPageF(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam String tenantId) {
        LambdaQueryWrapper<ExpressInfo> wrapper = Wrappers.<ExpressInfo>lambdaQuery();
        wrapper.eq(ExpressInfo::getTenantId, Integer.valueOf(tenantId));
        Page<ExpressInfo> farmInfoPage = expressInfoMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(farmInfoPage);
    }

    /**
     * 物流可视化，确定产品和重量
     *
     * @return
     */
    @GetMapping("/chatData")
    public Result<?> chatData(@RequestParam int tenantId) {
        LambdaQueryWrapper<ExpressInfo> queryWrapper = new LambdaQueryWrapper<>();
        //筛选出ExpressInfo表中tenantId和传递过来的数值相等的记录。
        queryWrapper.eq(ExpressInfo::getTenantId, tenantId);
        List<ExpressInfo> ExpressInfoList = expressInfoMapper.selectList(queryWrapper);
        List<ArrayCountUtils.BillsNums> billsNumsList = new ArrayList<>();
        for (ExpressInfo log : ExpressInfoList) {
            ArrayCountUtils.BillsNums billsNums = new ArrayCountUtils.BillsNums();
            billsNums.setId(log.getProName());
            billsNums.setSums(log.getProductWeight());
            billsNumsList.add(billsNums);
            billsNums = null;// 置空强制回收内存，以防数据量大内存泄露
        }
        List<ArrayCountUtils.BillsNums> res = merge(billsNumsList);
        return Result.success(res);
    }
    /**
     * 物流可视化，确定物流企业和重量
     *
     * @param tenantId
     * @return
     */
    @GetMapping("/chatDataEnterprise")
    public Result<?> chatData(@RequestParam String tenantId) {
        LambdaQueryWrapper<ExpressInfo> queryWrapper = new LambdaQueryWrapper<>();
        //筛选出ExpressInfo表中tenantId和传递过来的数值相等的记录。
        queryWrapper.eq(ExpressInfo::getTenantId, tenantId);
        List<ExpressInfo> ExpressInfoList = expressInfoMapper.selectList(queryWrapper);
        List<ArrayCountUtils.BillsNums> billsNumsList = new ArrayList<>();
        for (ExpressInfo log : ExpressInfoList) {
            ArrayCountUtils.BillsNums billsNums = new ArrayCountUtils.BillsNums();
            billsNums.setId(log.getFirmName());
            billsNums.setSums(log.getProductWeight());
            billsNumsList.add(billsNums);
            billsNums = null;// 置空强制回收内存，以防数据量大内存泄露
        }
        List<ArrayCountUtils.BillsNums> res = merge(billsNumsList);
        return Result.success(res);
    }

    /**
     * 根据物流批次码精确查询
     *

     * @return
     */
    @GetMapping("/findById")
    public Result<?> findByFarmId(@RequestParam("productCode") String productCode,
                                  @RequestParam String tenantId) {
        ExpressInfo expressInfo=new ExpressInfo();
        expressInfo.setProductCode(Integer.valueOf(productCode));
        expressInfo.setTenantId(Integer.valueOf(tenantId));

        ExpressInfo tmp = expressInfoMapper.selectByMultiId(expressInfo);
        List<ExpressInfo> farmInfoList = new ArrayList<>();
        if (tmp != null) {
            farmInfoList.add(tmp);
            return Result.success(farmInfoList);
        }
        return Result.error(0, "没有查询到相关数据");
    }


    /**
     * 根据产品名称模糊查询
     *
     * @param name
     * @return
     */
    @GetMapping("/findByProductName")
    public Result<?> findByProductName(
            @RequestParam("name") String name,
                                       @RequestParam String tenantId) {
        LambdaQueryWrapper<ExpressInfo> wrapper = Wrappers.<ExpressInfo>lambdaQuery();
        wrapper.like(ExpressInfo::getProName, name) .eq(ExpressInfo::getTenantId,tenantId);;
        List<ExpressInfo> expressInfoList = expressInfoMapper.selectList(wrapper);
        if (expressInfoList.size() > 0)
            return Result.success(expressInfoList);
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据公司名称模糊查询
     *

     * @return
     */
    @GetMapping("/findByFirmName")
    public Result<?> findByFirmName(@RequestParam("firmName") String firmName,
                                    @RequestParam String tenantId) {
        LambdaQueryWrapper<ExpressInfo> wrapper = Wrappers.<ExpressInfo>lambdaQuery();
        wrapper.like(ExpressInfo::getFirmName, firmName).eq(ExpressInfo::getTenantId,tenantId);
        List<ExpressInfo> expressInfoList = expressInfoMapper.selectList(wrapper);
        if (expressInfoList.size() > 0)
            return Result.success(expressInfoList);
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据code删除信息
     *

     * @return
     */
    @DeleteMapping("/delById/")
    public Result<?> delete(@RequestParam String productCode,
                            @RequestParam String tenantId) {
        ExpressInfo expressInfo=new ExpressInfo();
        expressInfo.setProductCode(Integer.valueOf(productCode));
        expressInfo.setTenantId(Integer.valueOf(tenantId));
        int res = expressInfoMapper.deleteByMultiId(expressInfo);
        if (res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }

    /**
     * 添加信息
     * @param productCode
     * @param proName
     * @param firmName
     * @param firmTel
     * @param productWeight
     * @param destination
     * @param inCharge
     * @param outCharge
     * @param dateline
     * @param outDate
     * @param note
     * @return
     */
    @GetMapping(value = "/add")
    public Result<?> saveE(@RequestParam String productCode,
                           @RequestParam String proName,
                           @RequestParam String firmName,
                           @RequestParam String firmTel,
                           @RequestParam String productWeight,
                           @RequestParam String destination,
                           @RequestParam String inCharge,
                           @RequestParam String outCharge,
                           @RequestParam String dateline,
                           @RequestParam String outDate,
                           @RequestParam String note,
                           @RequestParam String tenantId
                           ) {
        if (productCode.isEmpty() || proName.isEmpty() || firmName.isEmpty() || firmTel.isEmpty() || productWeight.isEmpty()||
                destination.isEmpty() || inCharge.isEmpty() || outCharge.isEmpty() || dateline.isEmpty() || outDate.isEmpty() ) {
            return Result.error(0, "信息不能为空");
        }
        ExpressInfo expressInfo = new ExpressInfo(Integer.valueOf(productCode),proName,firmName,firmTel,Double.valueOf(productWeight),destination,inCharge,outCharge,dateline,outDate,note,Integer.valueOf(tenantId));
        expressInfoMapper.insert(expressInfo);
        return Result.success();
    }

    /**
     * 根据主键修改信息
     * @param productCode
     * @param proName
     * @param firmName
     * @param firmTel
     * @param productWeight
     * @param destination
     * @param inCharge
     * @param outCharge
     * @param dateline
     * @param outDate
     * @param note
     * @return
     */
    @GetMapping(value = "/update")
    public Result<?> updateById(@RequestParam String productCode,
                           @RequestParam String proName,
                           @RequestParam String firmName,
                           @RequestParam String firmTel,
                           @RequestParam String productWeight,
                           @RequestParam String destination,
                           @RequestParam String inCharge,
                           @RequestParam String outCharge,
                           @RequestParam String dateline,
                           @RequestParam String outDate,
                           @RequestParam String note,
                                @RequestParam String tenantId
    ) {
        if (productCode.isEmpty() || proName.isEmpty() || firmName.isEmpty() || firmTel.isEmpty() || productWeight.isEmpty()||
                destination.isEmpty() || inCharge.isEmpty() || outCharge.isEmpty() || dateline.isEmpty() || outDate.isEmpty()) {
            return Result.error(0, "信息不能为空");
        }
        ExpressInfo expressInfo = new ExpressInfo(Integer.valueOf(productCode),proName,firmName,firmTel,Double.valueOf(productWeight),destination,inCharge,outCharge,dateline,outDate,note,Integer.valueOf(tenantId));
        expressInfoMapper.updateByMultiId(expressInfo);
        return Result.success();
    }

}
